package Bus;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import Main.JDBCConnecter;

/**
 * Fetch the train's information with bus's name, and save it into databaase
 * 
 * ����г��ĳ��Σ�������ץȡ���г�����Ϣ�����������ֶ�
 * 
 * bus_name					����
 * station_number			ʼ��վ
 * station_name				�յ�վ
 * 
 * ����Ϊһ����¼�����浽��ݿ�� beijing_bus_line����
 * 
 * @author buju.hh
 *
 */
public class BusStation {
	
	private String bus_info;
	private String bus_name;
	
	private String NA = "NULL";
	private boolean DEBUG = true;
	
	public BusStation(String busName, String busInfo) {
		
		this.bus_info = busInfo;
		this.bus_name = busName;
		
		if((DEBUG)){
			System.out.println("\n-------- DEBUG BusStation START--------");
		}
		
		fetchBusStationInfo();
		
		if(DEBUG) System.out.println("-------- DEBUG BusStation END--------\n\n");
	}
	
	private void fetchBusStationInfo(){
		String user = "root";
		String pwd = "root";
		String url = "jdbc:mysql://localhost:3306/bjbus";
		String tableName = "beijing_bus_station";
		String sqlstr;	
		JDBCConnecter mJDBCConnecter = new JDBCConnecter(url, user, pwd);
		mJDBCConnecter.connectDB();
		
		//String tag_bus_station = "this)'>.*?</a></td>";
		String tag_bus_station = "Stationonclick.*?</a></td>";
		PatternCompiler compiler = new Perl5Compiler();
		PatternMatcher matcher = new Perl5Matcher();
		org.apache.oro.text.regex.Pattern pattern = null;
		PatternMatcherInput input = new PatternMatcherInput(this.bus_info);
		
		try {
			pattern = compiler.compile(tag_bus_station);
		} catch (MalformedPatternException e) {
			e.printStackTrace();
		}
		
		String station_number, station_name;
		while(matcher.contains(input, pattern)) {
        	MatchResult result = matcher.getMatch();
        	String temp = result.group(0);	           
        	station_number = this.get_station_number(temp);
        	station_name = this.get_station_name(temp);
        	
        	StringBuilder sb_sql = new StringBuilder();
            sb_sql.append("insert into ");
            sb_sql.append(tableName);
            sb_sql.append("(bus_name,station_number,station_name) ");
            sb_sql.append("values('");
            sb_sql.append(bus_name);
            sb_sql.append("',");
            sb_sql.append(station_number);
            sb_sql.append(",'");
            sb_sql.append(station_name);
            sb_sql.append("');");
            sqlstr = sb_sql.toString();

    		mJDBCConnecter.executeInsert(sqlstr);
        }
		
		mJDBCConnecter.closeDB();
	}
	
	private String get_station_number(String str){
		Pattern pattern1 = Pattern.compile("<b>[0-9]*?</b>"); 
		Matcher matcher1 = pattern1.matcher(str);
		String ret = null;
		
		if(matcher1.find()){
			String temp = matcher1.group();
			ret = temp.substring(3, temp.lastIndexOf("</b>"));
			if(DEBUG) System.out.println("station_number:" + ret);
		}else{
			ret = this.NA;
			if(DEBUG) System.err.println("station_number: FAIL");
		}
		
		return ret;
	}
	
	private String get_station_name(String str){
		Pattern pattern1 = Pattern.compile("</b>.*?</a></td>"); 
		Matcher matcher1 = pattern1.matcher(str);
		String ret = null;
		
		if(matcher1.find()){
			String temp = matcher1.group();
			ret = temp.substring(4, temp.lastIndexOf("</a></td>"));
			if(DEBUG) System.out.println("station_name:" + ret);
		}else{
			ret = this.NA;
			if(DEBUG) System.err.println("station_name: FAIL");
		}
		
		return ret;
	}
}
